﻿using System;
using System.ServiceProcess;
using System.Windows.Forms;
using System.Threading;
using BloggerAutoPosterService.Engine.Utils;
using BloggerAutoPosterService.Engine.Core;

namespace BloggerAutoPosterService
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            Application.ThreadException += threadExceptionFunction;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += unhandledExceptionFunction;

            var servicesToRun = new ServiceBase[] 
                { 
                    new Main() 
                };
            ServiceBase.Run(servicesToRun);
        }

        private static void unhandledExceptionFunction(object sender, UnhandledExceptionEventArgs e)
        {
            if (e != null) new Logger(new ServiceConfig(useApplicationPath: false)).WriteLine((Exception)e.ExceptionObject);
        }

        private static void threadExceptionFunction(object sender, ThreadExceptionEventArgs e)
        {
            if (e != null) new Logger(new ServiceConfig(useApplicationPath: false)).WriteLine(e.Exception);
        }
    }
}
